package Application.Components.DiningList;

import java.awt.Color;
import java.awt.Graphics;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Random;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import Interfaces.IDrawableComponent;

public class DiningList implements IDrawableComponent{
	public static final String DININGLIST_FILE	=	"\\\\gandalf.sospost.local\\workspace\\drahomir.karchnak\\ScreenSaverSSINFIS\\res\\diningListTest.xml";
	
	public static final String NODE_DATE_FROM 	=	"pocatek";
	public static final String NODE_DATE_TO		=	"konec";
	
	public static final String NODE_NAME_MONDAY		=	"pondeli";
	public static final String NODE_NAME_TUESDAY	=	"utery";
	public static final String NODE_NAME_WEDNESDAY	=	"streda";
	public static final String NODE_NAME_THURSDAY	=	"ctvrtek";
	public static final String NODE_NAME_FRIDAY		=	"patek";
	
	public static final int MEAL_INDEX_MONDAY		=	0;
	public static final int MEAL_INDEX_TUESDAY		=	1;
	public static final int MEAL_INDEX_WEDNESDAY	=	2;
	public static final int MEAL_INDEX_THURSDAY		=	3;
	public static final int MEAL_INDEX_FRIDAY		=	4;
	
	private int windowWidth, windowHeight;
	private String dateFrom, dateTo;
	
	private Meal[] meals = new Meal[5];
	
	public DiningList(int winWidth, int winHeight){
		this.windowWidth	= winWidth;
		this.windowHeight	= winHeight;
	}

	@Override
	public void draw(Graphics g) {
		g.setColor(new Color(255,0,255));
		g.fillRect(0, 0, windowWidth, windowHeight);
	}
	
	public void load(){
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
		
		try {
			db = dbf.newDocumentBuilder();
			Document d = db.parse(new File(DININGLIST_FILE));
			
			Element e = d.getDocumentElement();
			
			NodeList nl = e.getChildNodes();
			
			this.dateFrom	= e.getAttributeNode(NODE_DATE_FROM).getTextContent();
			this.dateTo 	= e.getAttributeNode(NODE_DATE_TO).getTextContent();
			
			for (int i = 0; i < nl.getLength(); i++) {
				Node n = nl.item(i);
				
				Node nodeMeal;		
				switch(n.getNodeName()){	//switching over day
					case	NODE_NAME_MONDAY:
							nodeMeal = n.getChildNodes().item(0);
							meals[MEAL_INDEX_MONDAY] = new Meal(nodeMeal.getTextContent());
						break;
						
					case	NODE_NAME_TUESDAY:
							nodeMeal = n.getChildNodes().item(0);
							meals[MEAL_INDEX_TUESDAY] = new Meal(nodeMeal.getTextContent());
						break;
						
					case	NODE_NAME_WEDNESDAY:
							nodeMeal = n.getChildNodes().item(0);
							meals[MEAL_INDEX_WEDNESDAY] = new Meal(nodeMeal.getTextContent());
						break;
						
					case	NODE_NAME_THURSDAY:
							nodeMeal = n.getChildNodes().item(0);
							meals[MEAL_INDEX_THURSDAY] = new Meal(nodeMeal.getTextContent());
						break;
						
					case	NODE_NAME_FRIDAY:
							nodeMeal = n.getChildNodes().item(0);
							meals[MEAL_INDEX_FRIDAY] = new Meal(nodeMeal.getTextContent());
						break;
					
					default:
						
						break;
				}
			}
			
			System.out.println("Dining list from: " + dateFrom + " to: " + dateTo);
			System.out.println(meals[MEAL_INDEX_MONDAY].getName());
			System.out.println(meals[MEAL_INDEX_TUESDAY].getName());
			System.out.println(meals[MEAL_INDEX_WEDNESDAY].getName());
			System.out.println(meals[MEAL_INDEX_THURSDAY].getName());
			System.out.println(meals[MEAL_INDEX_FRIDAY].getName());
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		DiningList d = new DiningList(1024, 768);
		d.load();
	}
}
